package com.probox.modules.controller.customer;

import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.PageDomain;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.core.web.page.TableSupport;
import com.probox.common.entity.service.customer.ICustomerLevelService;
import com.probox.common.entity.vo.customer.CustomerLeveListlVo;
import com.probox.common.entity.vo.customer.CustomerLevelPrivleageVo;
import com.probox.common.entity.vo.customer.CustomerLevelVo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.customer.api.domain.CustomerLevel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 客户等级Controller
 *
 * @author zsy
 * @date 2021-05-27
 */
@Api(value = "客户模块", tags = {"客户等级后台操作接口"})
@RestController
@RequestMapping("/customer/level")
public class CustomerLevelController extends BaseController {
    @Autowired
    private ICustomerLevelService customerLevelService;

    /**
     * 查询客户等级列表
     */
    @ApiOperation(value = "查询客户等级列表")
    @PreAuthorize(hasPermi = "customer:level:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerLevel customerLevel) {
        startPage();
        List<CustomerLevel> list = customerLevelService.queryList(customerLevel);
        return getDataTable(list);
    }

    /**
     * 等级下拉框
     *
     * @param customerLevel
     * @return
     */
    @GetMapping("/listSelect")
    public AjaxResult listSelect(CustomerLevel customerLevel) {
        return AjaxResult.success(customerLevelService.queryList(customerLevel));
    }

    /**
     * 导出客户等级列表
     */
    @ApiOperation(value = "导出客户等级列表")
    @PreAuthorize(hasPermi = "customer:level:export")
    @Log(title = "客户等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerLevel customerLevel) throws IOException {
        List<CustomerLevel> list = customerLevelService.queryList(customerLevel);
        ExcelUtil<CustomerLevel> util = new ExcelUtil<CustomerLevel>(CustomerLevel.class);
        util.exportExcel(response, list, "客户等级数据");
    }

    /**
     * 获取客户等级详细信息
     */
    @ApiOperation(value = "获取客户等级详细信息")
    @PreAuthorize(hasPermi = "customer:level:query")
    @GetMapping(value = "/{levelId}")
    public AjaxResult getInfo(@PathVariable("levelId") Long levelId) {
        return AjaxResult.success(customerLevelService.selectCustomerLevelById(levelId));
    }

    /**
     * 新增客户等级
     */
    @ApiOperation(value = "新增客户等级")
    @PreAuthorize(hasPermi = "customer:level:add")
    @Log(title = "客户等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerLevel customerLevel) {
        return toAjax(customerLevelService.insertCustomerLevel(customerLevel));
    }

    /**
     * 修改客户等级
     */
    @ApiOperation(value = "修改客户等级")
    @PreAuthorize(hasPermi = "customer:level:edit")
    @Log(title = "客户等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerLevel customerLevel) {
        return toAjax(customerLevelService.updateCustomerLevel(customerLevel));
    }

    /**
     * 删除客户等级
     */
    @ApiOperation(value = "删除客户等级")
    @PreAuthorize(hasPermi = "customer:level:remove")
    @Log(title = "客户等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{levelIds}")
    public AjaxResult remove(@PathVariable Long[] levelIds) {
        return toAjax(customerLevelService.deleteCustomerLevelByIds(levelIds));
    }

    /**
     * 查询客户免费客户模式等级列表
     */
    @ApiOperation(value = "查询客户免费客户模式等级列表")
    @PreAuthorize(hasPermi = "customer:level:list")
    @GetMapping("/levelInfolist")
    public TableDataInfo levelInfolist(CustomerLeveListlVo customerLevelVo) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageSize = pageDomain.getPageSize();
        Integer pageNum = (pageDomain.getPageNum() - 1) * pageSize;
        List<CustomerLeveListlVo> list = customerLevelService.getLevelInfoList(customerLevelVo, pageNum, pageSize);
        TableDataInfo dataTable = getDataTable(list);
        //Long count = customerLevelService.getLevelInfoListCount(customerLevelVo);
        //dataTable.setTotal(count);
        return dataTable;
    }

    /**
     * 查询客户免费客户模式等级列表
     */
    @ApiOperation(value = "查询客户免费客户模式等级列表")
    @PreAuthorize(hasPermi = "customer:level:list")
    @GetMapping("/memberLevelList")
    public TableDataInfo memberLevelList() {
        CustomerLevel customerLevel = new CustomerLevel();
        //customerLevel.setDataStatus("1");
        List<CustomerLevel> list = customerLevelService.queryList(customerLevel);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    /**
     * 修改客户等级启动状态
     */
    @ApiOperation(value = "修改客户等级启动状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "levelId", value = "等级ID", paramType = "bigint", required = true),
            @ApiImplicitParam(name = "startStatus", value = "启动状态（0：未启动，1：已启动）", paramType = "String", required = true),
    })
    @PreAuthorize(hasPermi = "customer:level:edit")
    @Log(title = "修改客户等级启动状态", businessType = BusinessType.UPDATE)
    @PutMapping("/UpdateStartStatus")
    public AjaxResult UpdateStartStatus(@RequestBody CustomerLevel customerLevel) {
        return toAjax(customerLevelService.updateCustomerLevel(customerLevel));
    }

    /**
     * 查询客户付费客户模式等级列表
     */
    @ApiOperation(value = "查询客户付费客户模式等级列表")
    @PreAuthorize(hasPermi = "customer:level:list")
    @GetMapping("/PaylevelInfolist")
    public TableDataInfo paylevelInfolist(CustomerLeveListlVo customerLevelVo) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageSize = pageDomain.getPageSize();
        Integer pageNum = (pageDomain.getPageNum() - 1) * pageSize;
        List<CustomerLeveListlVo> list = customerLevelService.getPayLevelInfoList(customerLevelVo, pageNum, pageSize);
        TableDataInfo dataTable = getDataTable(list);
        Long count = customerLevelService.getPayLevelInfoListCount(customerLevelVo);
        dataTable.setTotal(count);
        dataTable.setRows(list);
        return dataTable;
    }

    /**
     * 查询详细客户等级信息
     */
    @ApiOperation(value = "查询详细客户等级信息")
    @PreAuthorize(hasPermi = "customer:level:list")
    @GetMapping("/levelInfoDetail/{levelId}")
    public AjaxResult levelInfoDetail(@PathVariable Long levelId) {
        CustomerLevelVo customerLevelVo = customerLevelService.getlevelInfoDetail(levelId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("customerLevel", customerLevelVo);
        return ajax;
    }

    /**
     * 新增客户等级以及权益信息
     */
    @ApiOperation(value = "新增客户等级权益", notes = "新增客户等级以及权益信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerLevelVo", value = "客户等级Vo类", paramType = "body", required = true, dataType = "CustomerLevelVo"),
            @ApiImplicitParam(name = "levelName", value = "客户等级姓名", paramType = "String", required = true),
            @ApiImplicitParam(name = "caedFace", value = "卡面(0:背景色，1：背景图)", paramType = "String", required = true),
            @ApiImplicitParam(name = "levelColor", value = "卡面为0时传值", paramType = "String"),
            @ApiImplicitParam(name = "levelImage", value = "卡面为1时传值", paramType = "String"),
            @ApiImplicitParam(name = "minScore", value = "最小积分", paramType = "bigint", required = true),
            @ApiImplicitParam(name = "fansNumber", value = "粉丝数量", paramType = "bigint", required = true),
            @ApiImplicitParam(name = "performance", value = "业绩", paramType = "bigint", required = true),
            @ApiImplicitParam(name = "customerPrivileges", value = "客户权益Vo集合", paramType = "String"),
            @ApiImplicitParam(name = "privilegeUid", value = "客户权益下的权益id", paramType = "bigint"),
            @ApiImplicitParam(name = "privilegeStatus", value = "客户权益状态(1：有效,0：无效)'", paramType = "String"),
            @ApiImplicitParam(name = "privilegeValue", value = "客户权益对应的值", paramType = "String"),
            @ApiImplicitParam(name = "customerPrivlege", value = "普通权益名称", paramType = "String"),
            @ApiImplicitParam(name = "upgradeGiftBag", value = "升级礼包权益名称", paramType = "String"),
            @ApiImplicitParam(name = "levelPrivileageGives", value = "等级权益赠送集合(当为赠品时传值)", paramType = "String"),
            @ApiImplicitParam(name = "giveGiftId", value = "赠品ID", paramType = "String"),
            @ApiImplicitParam(name = "giveType", value = "赠品类型", paramType = "String"),
            @ApiImplicitParam(name = "paymentRules", value = "付费模式规则", paramType = "String")
    })
    @PreAuthorize(hasPermi = "customer:level:add")
    @Log(title = "客户等级权益信息", businessType = BusinessType.INSERT)
    @PostMapping("/saveLevelPrivileageInfo")
    public AjaxResult addLevelPrivileage(@RequestBody CustomerLeveListlVo customerLevelVo) {
        return toAjax(customerLevelService.saveLevelPrivileageInfoNew(customerLevelVo));
    }


    @PreAuthorize(hasPermi = "customer:level:edit")
    @Log(title = "客户等级权益信息", businessType = BusinessType.UPDATE)
    @PostMapping("/UpdateLevelPrivileageInfo")
    public AjaxResult updateLevelPrivileage(@RequestBody CustomerLeveListlVo customerLevelVo) {
        return toAjax(customerLevelService.updateLevelPrivileageInfoNew(customerLevelVo));
    }

    @PreAuthorize(hasPermi = "customer:level:list")
    @GetMapping("/levelPrivileageInfo")
    public AjaxResult PrivileageInfo() {
        AjaxResult ajaxResult = new AjaxResult();
        List<CustomerLevelPrivleageVo> customerLevelPrivleageVo = customerLevelService.getLevelPrivileageInfo();
        ajaxResult.put("customerLevelPrivleageVos", customerLevelPrivleageVo);
        return ajaxResult;
    }

    /**
     * 查询所有付费等级列表
     */
    @GetMapping("/getPayLevelInfo")
    public List<CustomerLevel> getPayLevelInfo() {
        List<CustomerLevel> customerLevelList = customerLevelService.getPayLevelInfo();
        return customerLevelList;
    }

    /**
     * 获取可以选择的等级
     *
     * @param levelId
     * @return
     */
    @GetMapping("/getLevel")
    public AjaxResult getLevel(Long levelId) {
        return AjaxResult.success(customerLevelService.getLevel(levelId));
    }

}
